<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/6
 * Time: 11:25
 */

namespace App\Http\Controllers\Home;

use App\Model\ExceedShop;
use App\Model\ShopOrderComment;
use Illuminate\Http\Request;
use App\Model\ShopGoods;
use App\Model\ShopGoodsImg;
use App\Model\ShopGoodsClassify;
use App\Model\Members;
use App\Model\GoodsSpecsVal;
use App\Model\GoodsSpecsName;
use Illuminate\Support\Facades\Session;
use App\Model\MembersLevel;
class SuperShopController extends BaseController
{
    /**
     * 超级商城首页
     */
    public function index(Request $request)
    {
        //检测是否填写资料
        $user = session('home_user');
        if($user->isdown!=1) {
           return back()->withErrors('没有超级商城权限');
        }
        $UserModel = new Members();
        $userInfo = $UserModel->where('uid', $user['uid'])->first();
        Session::put('home_user' ,  $userInfo);
        if(empty($userInfo['phone']) || empty($userInfo['user_name'])){
            return redirect('home/member/userInfo')->withErrors('请完善个人资料');
        }
//        $userLeve = MembersLevel::where('id',$user['level'])->first();
//        if($user['credit3']>$userLeve['credit3']){
//            return redirect('home/straightShop/index')->withErrors('请复投');
//        }
        return view('Home.super.index');
    }
    /**
     * 购买页面
     */
    public function product()
    {
        return view('Home.super.product');
    }
    /**
     * 订单页面
     */
    public function order()
    {
        return view('Home.super.order');
    }
    /**
     * 超级商城商品表
     * @param Request $request
     * @return array
     * @throws \Exception
     */
    public function goods(Request $request)
    {
        $data = $request->all();
        if(empty($data)){
            return ['code'=>0,'data'=>'参数错误'];
        }
        switch ($data['class']){
            case 'select': //查询
                if (!empty($data['baodan'])){
                    $goodsDataWhere = [
                        'is_report'=>1,
                        'cid'=>6
                    ];
                    $classify = ShopGoods::where($goodsDataWhere)->get()->toArray();
                }else{
                    $table = ShopGoodsClassify::where(['type'=>3])->select('id');
                    $classify = $table->get()->toArray();
                }
                $pid = [];
                if (!empty($classify)) {
                    foreach ($classify as $key => $value) {
                        $pid[] = $value['id'];
                        $specsName = GoodsSpecsName::where( [ 'goods_id' => $value['id'] ] )->count();
                        $classify[$key]['specs_val']= 1;
                        if ($specsName){
                            $classify[$key]['specs_val']=2;
                        }

                    }
                } else {
                    return ['code'=>0,'data'=>'暂时还没有商品'];
                }
                //查询商品
                $wheres = [
                    'type'=>1,
                    'status'=>1,
                    'is_report'=>0,
                ];
                $shop = ShopGoods::where($wheres)->whereIn('cid',$pid)->get()->toArray();
//                //随机轮播图
//                $rotation = ShopGoods::select(
//                    'goods_thumb','id'
//                )
//                    ->where(['type' => 1,'status' => 1,])
//                    ->inRandomOrder()
//                    ->take(3)
//                    ->get()
//                    ->toArray();
                if ( $shop ) {
                    foreach ($shop as $k => $value) {
                        $img = ShopGoodsImg::where(['gid'=>$value['id']])->select('img')->first();
                        $shop[$k]['img'] = $img['img'];
                    }
                }

                /*上传时商品id改为17*/

                //查出该商品对应的所有规格名
                $GoodsSpecsName = GoodsSpecsName::where( [ 'goods_id' => 17 ] )->select( 'id', 'name' )->get()->toArray();
                //查出该商品的所有规格值
                $GoodsSpecsVal = [];
                if ( $GoodsSpecsName ) {
                    foreach ( $GoodsSpecsName as $k => $v ) {
                        $thisSpecsVal = GoodsSpecsVal::where( [ 'specs_id' => $v['id'] ] )->select( 'id', 'val', 'specs_id' )->get()->toArray();
                        array_push( $GoodsSpecsVal, [ 'specs_id' => $v['id'], 'vals' => $thisSpecsVal ] );
                    }
                }
                return ['code'=>1,'data'=>$shop, 'GoodsSpecsName' => $GoodsSpecsName, 'GoodsSpecsVal' => $GoodsSpecsVal,'goodsData'=>$classify];


                break;
            case 'add':    //添加 or 修改
                if ($data['goods']['id']) { //修改
                    $Rows = ShopGoods::where('id',$data['goods']['id'])->update($data['goods']);
                } else {
                    $data['createtime'] = time();
                    $Rows = ShopGoods::create($data['goods']);
                }
                //更新图片
                $goodsId = empty($data['goods']['id'])?$Rows->id : $data['goods']['id'];
                $goodsImgName = empty($data['goodsImgName'])?0:$data['goodsImgName'];
                $rico = $this->updateImg($goodsId,$goodsImgName);
                if ($Rows || $rico){
                    return ['code'=>1,'data'=>'设置成功'];
                } else {
                    return ['code'=>0,'data'=>'设置失败'];
                }
                break;
            case 'find':   //查询一条数据
            
                //查出该商品对应的所有规格名
                $GoodsSpecsName = GoodsSpecsName::where( [ 'goods_id' => $data['id'] ] )->select( 'id', 'name' )->get()->toArray();
                //查出该商品的所有规格值
                $GoodsSpecsVal = [];
                if ( $GoodsSpecsName ) {
                    foreach ( $GoodsSpecsName as $k => $v ) {
                        $thisSpecsVal = GoodsSpecsVal::where( [ 'specs_id' => $v['id'] ] )->select( 'id', 'val', 'specs_id' )->get()->toArray();
                        array_push( $GoodsSpecsVal, [ 'specs_id' => $v['id'], 'vals' => $thisSpecsVal ] );
                    }
                }

                $goods = ShopGoods::where('id',$data['id'])->first();
                if ($goods){
                    $orderData =  ShopOrderComment::where('goodsid',$data['id'])
                        ->join('shop_order','shop_order.id','=','shop_order_comment.orderid')
                        ->where('shop_order.status','>=',1)
                        ->join('members', 'shop_order_comment.uid', '=', 'members.uid')
                        ->select('members.name','shop_order.createtime')
                        ->orderBy('shop_order.createtime','DESC')
                        ->limit(5)
                        ->get();
                    foreach ($orderData as $k=>$v){
                        $orderData[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
                        $orderData[$k]['name'] = MOBILE_PHONE_HIDDEN($v['name']);
                    }
                }
                $goodsImgName = json_decode($goods['rotation_chart']);
                if ($goodsImgName){
                    $goodsList = array();
                    foreach ( $goodsImgName as $k => $v ){
                        $goodsList[$k]['img'] = $v;
                    }
                    $goodsImgName = $goodsList;
                }
                if (empty($goodsImgName)){
                    $goodsImgName = ShopGoodsImg::where('gid',$data['id'])->select('img')->get()->toArray();
                }
                //判断商品是否复投
//                $user = session('home_user');
                $type = 2;
//                $is_credit3 = ExceedShop::where('id',$user['super_level'])->value('credit5');
//                if ($user['credit3'] >= $is_credit3)
//                {
//                    $type = 1;
//                } else {
//                    $type = 2;
//                }
                if ($goods){
                    return ['code'=>1,'type' => $type,'data'=>['goods'=>$goods,'goodsImgName'=>$goodsImgName],'orderData' => $orderData,'GoodsSpecsName' => $GoodsSpecsName, 'GoodsSpecsVal' => $GoodsSpecsVal ];
                } else {
                    return ['code'=>0,'data'=>'设置失败'];
                }
                break;
            case 'delete': //删除
                if ($data['id']) {
                    $rico = ShopGoods::where('id',$data['id'])->update(['type' => -1]);
                    if ($rico){
                        return ['code'=>1,'data'=>'删除成功'];
                    } else {
                        return ['code'=>0,'data'=>'删除失败'];
                    }
                } else {
                    return ['code'=>0,'data'=>'数据错误'];
                }
                break;
        }
    }

    /**
     * 判断商品是否有规格
     * @param Request $request
     * @return array
     */

    public function isSpecs( Request $request)
    {
        $data = $request->all();
//        var_dump($data);die;
        $specsCount = GoodsSpecsName::where('goods_id',$data['gid'])->count();
        $GoodsSpecsName = GoodsSpecsName::where( [ 'goods_id' => $data['gid']] )->select( 'id', 'name' )->get()->toArray();
        $GoodsSpecsVal = [];
        if ($GoodsSpecsName){
            foreach ( $GoodsSpecsName as $k => $v ) {
                $thisSpecsVal = GoodsSpecsVal::where(['specs_id' => $v['id']])->select('id', 'val', 'specs_id')->get()->toArray();
                array_push($GoodsSpecsVal, ['specs_id' => $v['id'], 'vals' => $thisSpecsVal]);
            }
        }
        if ($specsCount > 0){
            return ['code' => 1,'GoodsSpecsName' => $GoodsSpecsName,'GoodsSpecsVal' => $GoodsSpecsVal];
        } else {
            return ['code' => 0];
        }
    }


}